package com.jwgf.decoration;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

public class SectionDecoration extends RecyclerView.ItemDecoration {
    private DecorationCallback callback;
    private int headerHeight = 80;

    public SectionDecoration(DecorationCallback callback) {
        this.callback = callback;
    }

    public void setHeaderHight(int height) {
        headerHeight = height;
    }
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int pos = parent.getChildAdapterPosition(view);

        if (pos == 0 || isFirstInGroup(pos)) {
            outRect.top = headerHeight;
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);

        int itemCount = state.getItemCount();
        int childCount = parent.getChildCount();

        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        String preGroupName;
        String currentGroupName = null;

        for (int i = 0; i < childCount; ++i) {
            View view = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(view);
            preGroupName = currentGroupName;
            currentGroupName = getGroupName(position);
            if (currentGroupName == null || TextUtils.equals(currentGroupName, preGroupName)) {
                continue;
            }
            int viewBottom = view.getBottom();
            int top = Math.max(headerHeight, view.getTop());
            if (position + 1 < itemCount) {
                String nextGroupName = getGroupName(position + 1);
                if (!currentGroupName.equals(nextGroupName) && viewBottom < top) {
                    top = viewBottom;
                }
            }

            View headerView = getGroupView(position);
            if (headerView != null) {
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, headerHeight);
                headerView.setLayoutParams(layoutParams);
                headerView.setDrawingCacheEnabled(true);
                headerView.measure(View.MeasureSpec.makeMeasureSpec(right-left, View.MeasureSpec.EXACTLY),
                        View.MeasureSpec.makeMeasureSpec(headerHeight, View.MeasureSpec.EXACTLY));
                headerView.layout(0,0,headerView.getMeasuredWidth(), headerView.getMeasuredHeight());
//                headerView.layout(0, 0, right, headerHeight);
                headerView.buildDrawingCache();
                Bitmap bitmap = headerView.getDrawingCache();
                c.drawBitmap(bitmap, left, top - headerHeight, null);
                headerView.setDrawingCacheEnabled(false);

            }
        }
    }

    private String getGroupName(int position) {
        if (callback != null) {
            return callback.getGroupName(position);
        } else {
            return null;
        }
    }

    private View getGroupView(int position) {
        if (callback != null) {
            return callback.getGroupView(position);
        } else {
            return null;
        }
    }

    private boolean isFirstInGroup(int pos) {
        if (pos == 0) {
            return true;
        } else {
            String prevGroupId = getGroupName(pos - 1);
            String groupId = getGroupName(pos);
            return !TextUtils.equals(prevGroupId, groupId);
        }
    }
}
